﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using exsportance.Silverlight.Common.Utils;

namespace exsportance.Silverlight.Forms
{
    /// <summary>
    /// Represents a single Form element consists of a label and a Control e.g. Username: TextBox
    /// </summary>
    public class FormElement
    {
        #region members
        private string _label;
        private FrameworkElement _control;
        private DependencyProperty _valueProperty;
        private Form _ownerForm = null;
        #endregion

        #region properties
        /// <summary>
        /// Name of FormElement
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// Label displayed to descripe control's value
        /// </summary>
        public string Label
        {
            get { return _label; }
        }

        /// <summary>
        /// Control e.g. TextBox or Combobox
        /// </summary>
        public FrameworkElement Control
        {
            get { return _control; }
        }

        /// <summary>
        /// Which property of Control is used for value evaluation
        /// </summary>
        public DependencyProperty ValueProperty
        {
            get { return _valueProperty; }
        }

        /// <summary>
        /// Current value of Control
        /// </summary>
        public object Value
        {
            get { return getControlValue(); }
            set { setControlValue(value); }
        }

        /// <summary>
        /// Gets the form which contains this element
        /// </summary>
        public Form OwnerForm
        {
            get { return _ownerForm; }
        }

        public FormBalloonTip BalloonTip { get; set; }
        #endregion

        #region constructor
        /// <summary>
        /// Creates a new FormElement
        /// </summary>
        /// <param name="ownerForm">The form for this element</param>
        /// <param name="name">Name of this element</param>
        /// <param name="label">A label shown to descripe the control's value. Example: Username</param>
        /// <param name="control">The Control for this form element</param>
        /// <param name="valueProperty">A property to get and set the control's value</param>
        public FormElement(Form ownerForm, string name, string label, FrameworkElement control, DependencyProperty valueProperty)
        {
            if (ownerForm == null)
                throw new ArgumentNullException("ownerForm", "A form must be provided!");

            if (name.IsNullOrEmpty())
                throw new ArgumentNullException("name", "A name must be provided!");

            if (control == null)
                throw new ArgumentNullException("control", "A control must be provided!");

            if (valueProperty == null)
                throw new ArgumentNullException("valueProperty", "A property must be provided!");

            Name = name;
            _label = label;
            _control = control;
            _valueProperty = valueProperty;
            _ownerForm = ownerForm;
        }

        /// <summary>
        /// Creates a new FormElement
        /// </summary>
        /// <param name="ownerForm">The form for this element</param>
        /// <param name="name">Name of this element</param>
        /// <param name="label">A label shown to descripe the control's value. Example: Username</param>
        /// <param name="control">The Control for this form element</param>
        /// <param name="valueProperty">A property to get and set the control's value</param>
        /// <param name="initialValue">A initial value</param>
        public FormElement(Form ownerForm, string name, string label, FrameworkElement control, DependencyProperty valueProperty, object initialValue)
            : this(ownerForm, name, label, control, valueProperty)
        {
            if (initialValue != null)
                Value = initialValue;
        }
        #endregion

        #region public methods
        /// <summary>
        /// Returns the row index for this element
        /// </summary>
        /// <returns></returns>
        public int GetRowIndex()
        {
            int result = _ownerForm.Elements.IndexOf(this) * 3;

            return result;
        }

        /// <summary>
        /// Returns the TextBlock-Control which contains the label
        /// </summary>
        /// <returns></returns>
        public TextBlock GetLabelControl()
        {
            return _ownerForm.GetLabelControl(this);
        }
        #endregion

        #region private methods
        /// <summary>
        /// Set the value of the control
        /// </summary>
        /// <param name="value"></param>
        private void setControlValue(object value)
        {
            _control.SetValue(_valueProperty, value);
        }

        /// <summary>
        /// Get the vallue of the control
        /// </summary>
        /// <returns></returns>
        private object getControlValue()
        {
            return _control.GetValue(_valueProperty);
        }
        #endregion
    }
}
